using Contoso_University_Web.Data;
using Microsoft.Extensions.DependencyInjection;

namespace Contoso_University_Web
{
    public static class InitialDataBase
    {
        public static void CreateDbIfNotExists(IServiceCollection services)
        {
            var provider = services.BuildServiceProvider();
            using (var scope = provider.CreateScope())
            {

                var servicesProvider = scope.ServiceProvider;
                try
                {
                    var context = servicesProvider.GetRequiredService<Data.SchoolContext>();
                    DbInitializer.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = servicesProvider.GetRequiredService<ILogger<Program>>();
                    logger.LogError(ex, "An error occured creating the DB");
                }
            }
        }
    }
}
